
export const KEY = 'goodsdata';

export var valueObj = {goodsid:0,count:0};

export function getItem(){
    var jsonString = localStorage.getItem(KEY);
    jsonString = jsonString || '[]';
    return JSON.parse(jsonString);
}

export function setItem(value){
    var jsonString = localStorage.getItem(KEY);
    jsonString = jsonString || '[]';
    // var data = getItem();
     var data = JSON.parse(jsonString);
     var flag = data.some(v=>{
     if(v.goodsid == value.goodsid){
            v.count += value.count;
            return true;
        }
     })
     if(!flag){
        data.push(value);
     }
    localStorage.setItem(KEY,JSON.stringify(data));
}

export function updata(obj){
    var data = getItem();
    console.log(obj);
    console.log(data);
    data.forEach(v=>{
        if(v.goodsid == obj.goodsid){
            v.count = obj.count;
        }
    })
    localStorage.setItem(KEY,JSON.stringify(data));
}

export function delGoods(id){
    var data = getItem();
    data.forEach((v,i)=>{
        if(v.goodsid == id){
            data.splice(i,1);
        }
    })
    localStorage.setItem(KEY,JSON.stringify(data));
}